<?php

declare(strict_types=1);

namespace app\common;

use app\api\library\Auth;
use think\facade\Cookie;
use think\exception\HttpResponseException;

class Frontend extends Api
{


    /**
     * 无需登录的方法
     * 访问本控制器的此方法，无需会员登录
     * @var array
     */
    protected array $noNeedLogin = [];

    /**
     * 无需鉴权的方法
     * @var array
     */
    protected array $noNeedPermission = [];
    /**
     * 权限类实例
     * @var Auth
     */
    protected $auth = null;

    public function initialize(): void
    {
        parent::initialize();
        $this->auth = Auth::instance();
        
        $token  = $this->request->header('token', 'token');
        if (!action_in_arr($this->noNeedLogin)) {
            $this->auth->init($token);
            if (!$this->auth->isLogin()) {
                $this->error(__('Please login first'), [
                    'routePath' => '/login'
                ], 302);
            }
        } elseif ($token) {
            try {
                $this->auth->init($token);
            } catch (HttpResponseException $e) {
            }
        }
    }
}
